package com.rongke.web;

import com.rongke.commons.JsonResp;
import com.rongke.enums.FlowTypeEnum;
import com.rongke.mapper.AccountMapper;
import com.rongke.model.*;
import com.rongke.service.*;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @version : Ver 1.0
 * @UserQuerstionController
 * @用户问卷调查Controller
 */
@RestController
@RequestMapping(value = "/api/userQuerstion")
@Transactional
@CrossOrigin
public class UserQuestionController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private UserQuestionService userQuestionService;
    @Autowired
    private UserQuestionnaireService userQuestionnaireService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private FlowDetailService flowDetailService;
    @Autowired
    private QuestionnaireService questionnaireService;

    /**
     * @param userQuestion
     * @return 返回值JsonResp
     * @添加用户问卷调查
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResp addUserQuerstion(@RequestBody UserQuestion userQuestion) {
        log.debug("添加用户问卷调查");
        userQuestionService.insert(userQuestion);
        return JsonResp.ok(userQuestion);
    }

    /**
     * @param userQuestion
     * @return 返回值JsonResp
     * @修改用户问卷调查
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updateUserQuerstion(@RequestBody UserQuestion userQuestion) {
        log.debug("修改用户问卷调查");
        userQuestionService.updateById(userQuestion);
        return JsonResp.ok(userQuestion);
    }

    /**
     * @param id
     * @return 返回值JsonResp
     * @根据id查找用户问卷调查
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectUserQuerstion(Long id) {
        log.debug("查找用户问卷调查");
        UserQuestion userQuestion = userQuestionService.selectById(id);
        return JsonResp.ok(userQuestion);
    }

    /**
     * @param userQuestion
     * @return 返回值JsonResp
     * @根据条件查找用户问卷调查
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectUserQuerstionByCondition(@RequestBody UserQuestion userQuestion) {
        log.debug("根据条件查找用户问卷调查");
        if (userQuestion == null)
            return JsonResp.toFail("参数不能为空");
        List<UserQuestion> userQuestionList = userQuestionService.selectByCondition(userQuestion);
        return JsonResp.ok(userQuestionList);
    }

    /**
     * 用户提交问卷
     *
     * @param userQuestion
     * @param uqnId        用户问卷id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/saveUserQues", method = RequestMethod.POST)
    public JsonResp saveUserQues(@RequestBody List<UserQuestion> userQuestion, Long uqnId) {
        log.debug("用户提交问卷");
        if (null == uqnId) {
            return JsonResp.toFail("参数不能为空");
        }
        for (UserQuestion u : userQuestion) {
            u.setAddtime(new Date());
        }
        userQuestionService.insertBatch(userQuestion);
        UserQuestionnaire uqn = new UserQuestionnaire(uqnId);
        uqn.setStatus(2);
        userQuestionnaireService.updateById(uqn);

        //用户获得奖励
        UserQuestionnaire userQuestionnaire = userQuestionnaireService.selectById(uqnId);
        Long userId = userQuestionnaire.getMuserId();
        Long qnId = userQuestionnaire.getQuestionnaireId();
        Questionnaire qn=questionnaireService.selectById(qnId);
        BigDecimal bonus=qn.getBonus();
        Account acc = new Account();
        acc.setMuserId(userId);
        Account account = accountMapper.selectOne(acc);
        BigDecimal duobi = account.getDuoBi();//余额（哆币）
        duobi=duobi.add(bonus);
        account.setDuoBi(duobi);
        accountService.updateById(account);
        String title = "有奖调查奖励";
        //**********************流水记录**********************//
        FlowDetail fd = new FlowDetail();
        fd.setMuserId(userId);
        fd.setFlowType(FlowTypeEnum.ORDER.getType());
        fd.setMoney(bonus);
        fd.setTitle(title);
        fd.setGmtDatetime(new Date());
        flowDetailService.insert(fd);
        return JsonResp.ok(userQuestion);
    }
}
